package com.orange.base.component.biztool.simulateddata.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.orange.base.component.biztool.simulateddata.service.SimulatedDataEnum;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataParam;

/**
 * 3级行政区划
 *
 * @author biankudingcha
 * @date: 2022-08-24
 * @since 0.5.0
 */
public class Area3Strategy extends AbstractJson {

    @Override
    public Object getValue(SimulatedDataParam param) {
        return getArea();
    }

    @SuppressWarnings("unchecked")
    public String getArea() {
        // 解析字符串
        List<Map<String, Object>> datas = (List<Map<String, Object>>) getData(null, "list");
        List<String> labelList = new ArrayList<String>();
        getRandomArea(datas, labelList);
        StringBuilder sb = new StringBuilder();
        for (String label : labelList) {
            sb.append(label);
        }
        return sb.toString();
    }

    public String getFileName() {
        return "AreaJson_2019__level_3.json";
    }

    @SuppressWarnings("unchecked")
    public void getRandomArea(List<Map<String, Object>> datas, List<String> labelList) {
        int index = getRandomNumber(0, datas.size() - 1);
        Map<String, Object> data = datas.get(index);
        labelList.add((String) data.get("label"));
        if (data.containsKey("children")) {
            Object children = data.get("children");
            if (children instanceof List) {
                List<Map<String, Object>> newDatas = (List<Map<String, Object>>) children;
                if (newDatas != null && newDatas.size() > 0) {
                    getRandomArea(newDatas, labelList);
                }
            }
        }
    }

    public String getType() {
        return SimulatedDataEnum.AREA3.getName();
    }

    public String getCaption() {
        return SimulatedDataEnum.AREA3.getCaption();
    }

}
